<template>
	<view class="orderPreview">
		<view class="address u-flex u-row-between ">
			<view class="add" v-if="addressList.length==0" @click="toaddress">
				请选择地址
			</view>
			<view class="left" v-if="addressList.length!=0" @click="toaddress">
				<view class="top">
					<view class="ad u-line-1">
						{{addressList[0].address}}
					</view>
					<view :class="addressList[0].is_default?'red':''">{{addressList[0].is_default?'默认':''}}</view>
				</view>
				<view class="num">
					{{addressList[0].name}} {{addressList[0].phone}}
				</view>
			</view>
			<view class="right">
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		<view class="shop">
			<view class="shop-item" v-for="(item,index) in cartsList" :key='item.id'>
				<view class="left">
					<u-image width="100%" height="200rpx" mode="heightFix" :src="item.goods.cover_url"></u-image>
				</view>
				<view class="right">
					<view class="title u-line-1">
						{{item.goods.title}}
					</view>
					<view class="foot">
						<view class="price">
							￥{{item.goods.price}}
						</view>
						<view class="num">
							x{{item.num}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="total">
				合计：<text>￥{{totalMoney}}</text>
			</view>
			<view class="sumbit">
				提交订单
			</view>
		</view>
	</view>
</template>

<script>
	import {
		orderPreview
	} from "../../common/http.api.js";
	export default {
		data() {
			return {
				addressList: [],
				cartsList: []
			}
		},
		computed: {
			totalMoney() {
				let total = 0;
				this.cartsList.forEach(item => {
					total += item.goods.price * item.num
				})
				return total.toFixed(2);
			}
		},
		async onLoad() {
			const res = await this.$u.api.orderPreview();
			if(uni.getStorageSync('currentAddressId')){
				const currentID = JSON.parse(uni.getStorageSync('currentAddressId')).id;
				let arr = []
				res.data.address.forEach(item => {
					if (item.id == currentID) {
						arr.push(item)
					}
				})
				this.addressList = arr;
			}
			this.cartsList = res.data.carts;
			console.log(res)
		},
		methods: {
			toaddress() {
				uni.navigateTo({
					url: "/pages/address/address?route=pages/cart/orderPreview"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.orderPreview {
		.address {
			padding: 10rpx 20rpx;
			background-color: #F1F1F1;

			.add {
				height: 120rpx;
				line-height: 120rpx;
				width: 90%;
			}

			.left {
				width: 90%;
				margin-left: 10rpx;
				.top{
					display: flex;
				}
                .red{
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: red;
					color: #FFFFFF;
				}
				.ad {
					margin-bottom: 40rpx;
					font-size: 30rpx;
					font-weight: bold;
				}
			}

			.right {
				margin-right: 10rpx;
			}
		}

		.shop {
			margin-top: 20rpx;

			&-item {
				margin-bottom: 20rpx;
				border-radius: 20rpx;
				display: flex;
				width: 100%;

				.left {
					margin-right: 20rpx;
				}

				.right {
					width: 60%;

					.title {
						font-size: 30rpx;
						font-weight: bold;
						margin-bottom: 100rpx;
					}

					.foot {
						display: flex;
						justify-content: space-between;
					}
				}
			}
		}

		.bottom {
			height: 50px;
			// background-color: #C0C0C0;
			box-shadow: 0 -10px 30px rgba($color: #000000, $alpha:0.1);
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			display: flex;
			justify-content: flex-end;
			line-height: 50px;

			.total {
				margin-right: 20rpx;

				text {
					color: red;
					font-weight: bold;
					font-size: 32rpx;
				}
			}

			.sumbit {
				width: 200rpx;
				text-align: center;
				color: #FFFFFF;
				background-color: #007AFF;
				// border-radius: 20rpx;
			}
		}
	}
</style>
